package com.whiteape.book.ui.adapter;

import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.whiteape.book.R;
import com.whiteape.book.core.manager.MediaStorageManager;
import com.whiteape.book.model.dto.CollectionMediaBean;
import com.whiteape.book.model.dto.MediaChapterBean;
import com.whiteape.book.model.dto.MediaRecordBean;
import com.whiteape.book.model.type.RVLayoutType;
import com.whiteape.book.utils.DaoDbHelper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MediaCollectionGroupAdapter extends BaseQuickAdapter<CollectionMediaBean, BaseViewHolder> {

    private List<CollectionMediaBean> selectedList = new ArrayList<>();
    private RVLayoutType layoutType = RVLayoutType.LIST;
    private boolean selectedStateEnabled = false;
    private Set<String> refreshingMediaSet = new HashSet<>();

    public MediaCollectionGroupAdapter(int layoutResId, @Nullable List<CollectionMediaBean> data) {
        super(layoutResId, data);
    }

    public void addRefreshingMedia(String media) {
        refreshingMediaSet.add(media);
    }

    public void removeRefreshingMedia(String media) {
        refreshingMediaSet.remove(media);
    }

    public void setLayoutType(RVLayoutType layoutType) {
        this.layoutType = layoutType;
    }

    public void setSelectedList(List<CollectionMediaBean> beanList) {
        selectedList = beanList;
    }

    public void setSelectedStateEnable(boolean enable) {
        selectedStateEnabled = enable;
    }

    @Override
    protected void convert(BaseViewHolder helper, CollectionMediaBean item) {

        item.__setDaoSession(DaoDbHelper.getInstance().getSession());
        helper.setVisible(R.id.iv_select, selectedStateEnabled);
        if (selectedList.contains(item)) {
            helper.setImageResource(R.id.iv_select, R.mipmap.icon_mine_has_selected);
            ((ImageView) helper.getView(R.id.iv_select)).setColorFilter(mContext.getResources().getColor(R.color.colorAccent));
        } else {
            ((ImageView) helper.getView(R.id.iv_select)).clearColorFilter();
            helper.setImageResource(R.id.iv_select, R.mipmap.icon_mine_not_select);
        }
        helper.setVisible(R.id.coll_book_iv_red_rot, item.getIfUpdated());
        helper.setText(R.id.coll_book_tv_name, item.getTitle());
        if (item.getIfLocal()) {
            //本地文件的图片
            Glide.with(mContext)
                    .load(R.mipmap.no_cover_portrait)
                    .fitCenter()
                    .into((ImageView) helper.getView(R.id.coll_book_iv_cover));
        } else {
            //书的图片
            helper.setText(R.id.coll_book_tv_source, "网源：" + item.getSiteSourceBean().getSiteName());
            Glide.with(mContext)
                    .load(item.getCoverUrl())
                    .placeholder(R.mipmap.no_cover_portrait)
                    .fitCenter()
                    .into((ImageView) helper.getView(R.id.coll_book_iv_cover));
        }

        MediaRecordBean recordBean = MediaStorageManager.getInstance().getMediaRecord(item.getId());
        item.__setDaoSession(DaoDbHelper.getInstance().getSession());
        int chapterIndex = recordBean == null ? 0 : recordBean.getChapter();
        List<MediaChapterBean> chapterBeanList = item.getMediaChapterList();
        if(refreshingMediaSet.contains(item.getId())){
            helper.setVisible(R.id.pb_chapter_loading,true);
            helper.setVisible(R.id.tv_chapter_not_read, false);
        }else {

            helper.setVisible(R.id.pb_chapter_loading,false);
            helper.setVisible(R.id.tv_chapter_not_read, true);

            int notReadYet = chapterBeanList.size() - chapterIndex - 1;
            notReadYet = notReadYet < 0 ? 0 : notReadYet;
            helper.setText(R.id.tv_chapter_not_read, notReadYet + "");
        }
        if (layoutType == RVLayoutType.LIST) {

            helper.setText(R.id.coll_book_tv_author, "作者：" + item.getAuthor());
//            if (!item.getIfLocal()) {
//                helper.setText(R.id.coll_book_tv_author, item.getLastUpdated());
//            } else {
//                helper.setText(R.id.coll_book_tv_lately_update, mContext.getString(R.string.read_progress));
//            }
            if (recordBean == null || recordBean.getChapter() < 0) {
                helper.setText(R.id.coll_book_tv_chapter, "尚未阅读");
                return;
            }


            if (chapterBeanList.isEmpty()) {
                helper.setText(R.id.coll_book_tv_chapter, "尚未阅读");
                return;
            }
            if (item.getMediaChapterList().size() <= chapterIndex || item.getMediaChapterList().get(chapterIndex) == null) {
                helper.setText(R.id.coll_book_tv_chapter, "尚未阅读");
                return;
            }
            helper.setText(R.id.coll_book_tv_chapter, "阅读至：" + item.getMediaChapterList().get(chapterIndex).getName());


        }

    }
}
